package com.huayue.point.controller;
import com.huayue.user.api.UserServiceApi;
import com.huayue.user.bean.UserDto;
import com.huayue.user.exception.IncorrectPasswordException;
import com.huayue.user.exception.UserAlreadyExistException;
import com.huayue.user.exception.UserNotExistException;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("user")
public class UserController implements BaseController {
    @Reference
    private UserServiceApi userServiceApi;

    /**
     * 用户登陆
     * @param request
     * @param no
     * @param password
     * @return
     * @throws Exception
     */
    @RequestMapping("login")
    public String login(HttpServletRequest request, String no, String password) throws Exception {
        UserDto userDto;
        try {
            userDto = userServiceApi.verifyIdentity(no, password);
            if (userDto != null) {
                request.getSession().setAttribute("user", userDto);
                return "登陆成功。";
            }
        } catch (UserNotExistException une) {
            throw  new UserNotExistException("工号不存在。");
        } catch (IncorrectPasswordException ipe) {
            throw  new IncorrectPasswordException("密码错误。");
        }
        return "未知异常。";
    }

    /**
     * 注销登录
     * @param request
     * @return
     */
    @RequestMapping("logout")
    public String logout(HttpServletRequest request)  {
        request.getSession().removeAttribute("user");
        return "注销成功。";
    }

    /**
     * 添加用户
     * @param request
     * @param no
     * @param password
     * @param name
     * @return
     * @throws Exception
     */
    @RequestMapping("add")
    public String add(HttpServletRequest request, String no, String password, String name) throws Exception {
        checkAdmin(request, "您不是管理员,无法添加用户。");
        try {
            userServiceApi.add(name, no, password);
            return "添加成功。";
        } catch (UserAlreadyExistException e) {
            throw new Exception("该工号已经存在。");
        }
    }

    /**
     * 查询用户信息，如果是管理员可以查询他人的信息，否则只能查询自己的信息
     * @param request
     * @param no
     * @return
     * @throws Exception
     */
    @RequestMapping("info")
    public UserDto info(HttpServletRequest request, @RequestParam(required = false)  String no) throws Exception {
        if(StringUtils.isNotEmpty(no)) {
            checkAdmin(request, "您不是管理员,不允许查询指定用户的信息。");
        } else {
            UserDto user = (UserDto)request.getSession().getAttribute("user");
            no = user.getNo();
        }
        UserDto userDto = userServiceApi.queryByNo(no);
        return userDto;
    }

    /**
     * 管理员删除用户
     * @param request
     * @param no
     * @return
     * @throws Exception
     */
    @RequestMapping("delete/{no}")
    public String delete(HttpServletRequest request, @PathVariable String no) throws Exception {
        checkAdmin(request, "您不是管理员,无法删除用户。");
        try {
            userServiceApi.removeByNo(no);
            return "删除成功。";
        } catch (UserNotExistException e) {
            throw new UserNotExistException("用户不存在。");
        }
    }

    /**
     * 管理员根据id修改用户信息
     * @param request
     * @param id
     * @param no
     * @param name
     * @return
     * @throws Exception
     */
    @RequestMapping("update")
    public String delete(HttpServletRequest request, Integer id, @RequestParam(required = false) String no, @RequestParam(required = false) String name) throws Exception {
        checkAdmin(request, "您不是管理员,无法修改用户信息。");
        try {
            UserDto userDto = new UserDto();
            userDto.setName(name);
            userDto.setNo(no);
            userDto.setId(id);
            userServiceApi.update(userDto);
            return "修改成功。";
        } catch (UserNotExistException e) {
            throw new UserNotExistException("用户不存在。");
        } catch (UserAlreadyExistException e) {
            throw new UserNotExistException("工号已存在。");
        }
    }

    /**
     * 修改用户密码
     * @param request
     * @param no
     * @param password
     * @return
     * @throws Exception
     */
    @RequestMapping("updatePassword")
    public String updatePassword(HttpServletRequest request,@RequestParam(required = false) String no, String password) throws Exception {
        if(StringUtils.isNotEmpty(no)) {
            checkAdmin(request, "您不是管理员,无法修改用户密码。");
        } else {
            UserDto user = (UserDto)request.getSession().getAttribute("user");
            no = user.getNo();
        }
        try {
            userServiceApi.updatePasswordByNo(no, password);
            return "密码修改成功。";
        } catch (UserNotExistException e) {
            throw new UserNotExistException("用户不存在。");
        }
    }
}
